﻿using ExcelSystem.Model;
using ExcelSystem.ToolClass;
using ExcelSystem.ViewModelsClass;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ExcelSystem.ViewModels
{
    public class BOMListViewModel : BindableBase
    {
        ExcelContext db;
        private ObservableCollection<BOMViewModel> bomsModel;
        public ObservableCollection<BOMViewModel> BomsModel
        {
            get { return bomsModel; }
            set { SetProperty(ref bomsModel, value); }
        }

        private OrderFilter orderFilter;

        /// <summary>
        /// 查询条件
        /// </summary>
        public OrderFilter OrderFilter
        {
            get { return orderFilter; }
            set { SetProperty(ref orderFilter, value); }
        }


        private DelegateCommand saveCommand;
        public DelegateCommand SaveCommand
        {
            get { return saveCommand; }
            set { SetProperty(ref saveCommand, value); }
        }

        private DelegateCommand<string> changeParCommand;
        /// <summary>
        /// 修改参数
        /// </summary>
        public DelegateCommand<string> ChangeParCommand
        {
            get { return changeParCommand; }
            set { SetProperty(ref changeParCommand, value); }
        }

        private DelegateCommand<object> changeFilterCommand;
        public DelegateCommand<object> ChangeFilterCommand
        {
            get { return changeFilterCommand; }
            set { SetProperty(ref changeFilterCommand, value); }
        }
        private readonly IRegionManager regionManage;
        private readonly IDialogService dialogService;
        public BOMListViewModel(IRegionManager regionManage,IDialogService dialogService)
        {
            //数据库文档
            db = ExcelContentFactory.GetInstance();
            //依赖注入
            this.regionManage = regionManage;
            this.dialogService = dialogService;
            SaveCommand = new DelegateCommand(Save);
            ChangeParCommand = new DelegateCommand<string>(ChangePar);
            ChangeFilterCommand = new DelegateCommand<object>(ChangeFilter);
            Init();
        }

        private void Init()
        {
            OrderFilter = new OrderFilter();
            OrderFilter.CurrentPage = 1;
            OrderFilter.PageSize = 50;
            GetBomModel();
        }
        private void ChangePar(string obj)
        {
            DialogParameters    keyValuePairs = new DialogParameters();
            keyValuePairs.Add("id", obj);
            dialogService.Show("BOMAdd", keyValuePairs,null);
        }

        private void Save()
        {
            db.SaveChanges();
        }

  

        private void ChangeFilter(object obj)
        {
            if (obj != null)
            {
                List<string> filter = new List<string>();
                object[] objects = obj as object[];
                foreach (var item in objects)
                {
                    filter.Add(item.ToString());
                }

                if (filter != null && filter.Count() == 2)
                {
                    switch (filter[0])
                    {
                        case "MaterialNo":
                            OrderFilter.MaterialNo = filter[1];
                            break;
                        case "OrderNo":
                            OrderFilter.OrderNo = filter[1];
                            break;
                        case "PageSize":
                            int cp = 0;
                            bool result = int.TryParse(filter[0], out cp);
                            if (result)
                            {
                                if (cp > OrderFilter.AllPage || cp <= 0)
                                {
                                    MessageBox.Show($"超出页码范围，请输入1-{OrderFilter.AllPage}的数字");
                                }
                            }
                            else
                            {
                                MessageBox.Show("请输入纯数字");
                            }
                            OrderFilter.PageSize = Convert.ToInt32(filter[1]);
                            break;
                        case "AllPage":
                            OrderFilter.AllPage = Convert.ToInt32(filter[1]);
                            break;
                        case "Count":
                            OrderFilter.Count = Convert.ToInt32(filter[1]);
                            break;
                        case "CurrentPage":
                            OrderFilter.CurrentPage = Convert.ToInt32(filter[1]);
                            break;

                    }

                }


            }
            GetBomModel();
        }
        public void GetBomModel()
        {
            //获取主表
            var boms = db.BOMs.Where(t => t == t);
            BomsModel = new ObservableCollection<BOMViewModel>();
            if (!string.IsNullOrEmpty(OrderFilter.MaterialNo))
                boms = boms.Where(t => t.MaterialNo.Contains(OrderFilter.MaterialNo));
            OrderFilter.Count = boms.Count();
            OrderFilter.AllPage = OrderFilter.Count / OrderFilter.PageSize + 1;
            var data = boms.ToList();
            data.ForEach(t => {
                BomsModel.Add(new BOMViewModel() { Data = t });
            });






        }


    }
}
